package com.sui.bigdata.sml.model.util;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.util.TimeUtils;
import com.feidee.fdhadoop.logCollect.LogCollectService;
import com.sui.bigdata.sml.model.model.InferenceRequest;
import com.sui.bigdata.sml.model.model.InferenceResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author caokaizhi
 * @Date 2019-12-6 17:37
 * @Description 将预测请求上报到 Hive dw.bdl_bd_sml_realtime_forecast 表
 * @Reviewer
 */
@Slf4j
public class LogCollector {

    private static String models = CustomConfiguration.getString(Constants.LOG_MODELS);

    private static String modelPaths = CustomConfiguration.getString(Constants.LOG_MODEL_PATHS);

    public static void uploadLog(InferenceRequest request, InferenceResponse response) {
        log.info("模型预测请求：{} -> {}", request.toString(), JSON.toJSONString(response.getResult()));
        Map<String, Object> resultLog = new HashMap<>(8);
        resultLog.put("busid", request.getBusId());
        resultLog.put("models", models);
        resultLog.put("model_paths", modelPaths);
        resultLog.put("features", request.getTotalFeatures());
        resultLog.put("result", response.getResult());
        resultLog.put("cost", response.getCost());
        resultLog.put("ts", TimeUtils.getCurrentTime());
        resultLog.put("traceid", request.getTraceId());
        LogCollectService.sendLog(CustomConfiguration.getString("log.apt", "bigdata"),
                Constants.LOG_BUSINESS_ID, resultLog);
    }
}
